﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.盘点.Service;
using DXApplicationTangche.UC.销售记录.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.盘点
{
    public partial class Form创建门店盘点计划 : DevExpress.XtraEditors.XtraForm
    {
        private String shopId;
        private String shopName;
        private StockInventoryPlanDTO stockInventoryPlanDTO;
        public Form创建门店盘点计划()
        {
            InitializeComponent();
        }
        public Form创建门店盘点计划(StockInventoryPlanDTO stockInventoryPlanDTO)
        {
            InitializeComponent();
            this.searchLookUpEdit选择门店.Properties.DataSource = 销售记录Service.get店铺();
            this.stockInventoryPlanDTO = stockInventoryPlanDTO;
            this.label盘点大计划编号.Text = "盘点大计划编号:" + this.stockInventoryPlanDTO.stockInventoryPlanCode;
            this.label盘点大计划时间.Text = "开始时间:" + this.stockInventoryPlanDTO.stockInventoryPlanStartDate.ToString("yyyy/MM/dd") +
                "结束时间:" + this.stockInventoryPlanDTO.stockInventoryPlanEndDate.ToString("yyyy/MM/dd");
            this.label盘点大计划备注.Text = "备注:" + this.stockInventoryPlanDTO.remarks;
        }
        #region 选择门店
        private void searchLookUpEdit选择门店_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择门店View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择门店.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.shopId = this.searchLookUpEdit选择门店.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.shopName = this.searchLookUpEdit选择门店.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    foreach (StockStoreInventoryPlanShowDTO dTO in stockInventoryPlanDTO.stockStoreInventoryPlanShowDTOS)
                    {
                        if (shopId.Equals(dTO.storeId))
                        {
                            MessageBox.Show("已有" + this.shopName + "盘点计划");
                            this.shopId = "";
                            this.shopName = "";
                            return;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择门店.ToolTip = null;
            searchLookUpEdit选择门店.EditValue = null;
        }
        private void searchLookUpEdit选择门店_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.shopName;
        }
        #endregion

        private void simpleButton新建盘点计划_Click(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(this.shopId)||
                String.IsNullOrEmpty(this.textBox负责人.Text)
                )
            {
                MessageBox.Show("请完善信息");
                return;
            }
            StockInventoryPlanDTO stockInventoryPlanDTO = 盘点Service.saveAddStoreInventoryPlan(
                this.stockInventoryPlanDTO.stockInventoryPlanCode,
                this.shopId,
                this.shopName,
                this.textBox负责人.Text,
                this.richTextBox备注.Text
                );
            if (String.IsNullOrEmpty(stockInventoryPlanDTO.stockInventoryPlanCode))
            {
                MessageBox.Show("创建失败");
            }
            else
            {
                MessageBox.Show("创建成功");
                this.Close();
            }
        }
    }
}
